// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var databind = require("databind");
	var css_phaseInventoryList = require("./phaseInventoryList.css");
	var tools = require("tools");
	var html_phase_days = require("./html_phase_days.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var financeId = "";
	var init = function(page) {
		financeId = page.params.financeId;
		router.init();
		tools.loadList("inquireBulkstockInventoryPageList.json", $("#phaseInventoryListTable"), $("#phaseInventoryListTpl"), {
			financeId: financeId
		}, function() {
			$(".detail").unbind("click").click(function(){
				router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
			});
			
			//根据放款通知单号分组
			var financeid = $("[financeid]");
			var loannoticeid = $("[loannoticeid]");
			var table = {};
			var len = loannoticeid.length;
			for (var i = 0; i < len; i++) {
				if (table[$(loannoticeid[i]).attr("loannoticeid")] == undefined) {
					var list = [];
					list.push(loannoticeid[i]);
					table[$(loannoticeid[i]).attr("loannoticeid")] = list;
				} else {
					table[$(loannoticeid[i]).attr("loannoticeid")].push(loannoticeid[i]);
				}
			}
			for (var param in table) {
				var inventoryId = $(table[param]).select("tr").attr("inventoryId");
				var str ="<div id='phaseDataContent'>";
					str +="<div id='phaseDataTitle'>";
					str += "<span><input class='phaseInventoryListCheckbox' type='radio' name='phaseInventoryListCheckbox'/></span>";
					str += "<span>放款通知单号：&nbsp;&nbsp;" + $(table[param]).select("tr").attr("loannoticeid") + "</span>";
					str += "</div>";
					str += "<div><table class='table' id='table_" +param+ "'>";
					str += "<tr>";
					str += "   <th>箱号</th>"
					str += "	   <th>箱型尺寸</th>";
					str += "	   <th>货物</th>";
					str += "	   <th>数量-体积-重量</th>";
					str += "	   <th>状态</th>";
					str += "       <th>起息日期</th>";
					str += "	   <th>当前计息开始日期</th>";
					str += "	   <th>操作</th>";
					str += "</tr>";
					str+="</table></div>";
					str += "</div>";
					$("#newTable").append(str);
					var selector = "#table_" + param;
					$(selector).append($(table[param]));
			}
			$("#phaseInventoryListTable").html("");
			
			
			$(".phaseInventoryListCheckbox").unbind("click").click(function(){
				var checks = [];
				$("#phaseInventoryList .inventoryIds").html("");
				$.each($(".phaseInventoryListCheckbox:checked"), function() {
					var $this = $(this);
					var tr = $this.parent().parent().next().children().find("tr[inventoryId]");
					for (var i = 0; i < tr.length; i++) {
						var value = $((tr)[i]).attr("inventoryId");
						var str = '<input type="hidden" value="' + value + '" name="inventoryId" />';
						$("#phaseInventoryList .inventoryIds").append(str);
					}
				});
			});
			
			$("#createInterestSettlementLink").unbind("click").click(function(){
				if($(".phaseInventoryListCheckbox:checked").length <= 0){
					base.alert(2, "请勾选需要收息的货箱！");
					return;
				}
				$("#popdialog").html(html_phase_days);
				dialog.showDialog($("#transport_phase_days"));
				
				$("#transport_phase_days .sure_no").unbind("click").click(function(){
					dialog.hideDialog($("#transport_phase_days"));
				});
				
				$("#transport_phase_days .sure_yes").unbind("click").click(function(){
					if($.trim($("#transport_phase_days .days").val())!=""){
						$("#phaseInventoryList .inventoryIds").append($("#transport_phase_days .days").clone().attr("type", "hidden"));
						$("#phaseInventoryList .inventoryIds").append('<input type="hidden" name="financeId" value="'+ financeId +'" />');
						databind.submitDom($(".inventoryIds"), function(){
							base.alert(1, "收息成功！");
							router.route("financing/financingDetailInfo.html?financeId=" + financeId);
						});
						dialog.hideDialog($("#transport_phase_days"));
					}else{
						base.alert(3, "请输入收息天数！");
					}
				});
			});
		});

		initEvent();
	};

	var initEvent = function() {

	};

	//			//查询融资货箱列表
	//			var inquireBulkstockInventoryPageList = function(financeId) {
	//				base.fnAjax({
	//					action: "inquireFinanceSupervisionNotice.json",
	//					oData: {
	//						financeId: financeId
	//					}, //传递参数
	//					fnSuccess: function(data) {
	//						if (data.status == 200) {
	//							databind.fillForm(".supervisionInformContent", dataFinancingDetail.datas[0]);
	//							if (data.datas.length > 0 && data.datas[0].supervisionNoticeId != 0) {}
	//
	//						} else {
	//							base.alert(2, "信息获取失败");
	//						}
	//					}
	//				});
	//
	//			};

	return {
		init: init
	}
});